﻿<%@ WebHandler Language="C#" Class="PortletHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Util;
using EHSubmit.Logic.Relationship;
using EHSubmit.Logic.Services;

/// <summary>
/// Ajax首页处理类
/// </summary>
public class PortletHandler : BaseAjaxHandler {

    protected override void Mapping()
    {
        //添加业务逻辑方法
        OprationMap["GetMessage"] = "GetMessage";
        OprationMap["GoalMessage"] = "GoalMessage";
        OprationMap["GetWeather"] = "GetWeather";
    }

    public void GetMessage()
    {
        try
        {
            if (Validate(ERole.Student))
            {
                String[] MessageNum = new string[2];
                MessageManager MessageManager = new MessageManager();
                MessageNum = MessageManager.MessageNumByUser(CurrentUser.ID);       //返回的消息数目，用户与系统
                IRelationshipManager manager = RelationshipFactory.GetInstance();
                IList<EHSubmit.Model.Goal> goals = manager.GetGoalsOfStudent(CurrentUser.ID);
                int count = goals.Count;                //待交的作业数目
                IsSuccess = true;
                ResponseText = "{success:true,data:[{\"UserNum\":\"" + MessageNum[0] + "\",\"AdminNum\":\"" + MessageNum[1] + "\",\"HomeWorkNum\":\"" + count + "\"}]}";
            }
            else if (Validate(ERole.Teacher))
            {
                String[] MessageNum = new string[2];
                MessageManager MessageManager = new MessageManager();
                MessageNum = MessageManager.MessageNumByUser(CurrentUser.ID);       //返回的消息数目，用户与系统
                int UnViewedHomework = 0;
                HomeworkManager HomeworkManager = new HomeworkManager();
                UnViewedHomework = HomeworkManager.UnViewedHomeworkNum(CurrentUser.ID);
                IsSuccess = true;
                ResponseText = "{success:true,data:[{\"UserNum\":\"" + MessageNum[0] + "\",\"AdminNum\":\"" + MessageNum[1] + "\",\"HomeWorkNum\":\"" + UnViewedHomework + "\"}]}";
            }
        }
        catch (Exception e)
        {
            Message = e.Message;
        }
    }

    /// <summary>
    /// 列出还未交的作业的提示信息
    /// </summary>
    public void GoalMessage()
    {
        if (Validate(ERole.Student))
        {
            IRelationshipManager manager = RelationshipFactory.GetInstance();
            /// 根据学生ID,取需要提交的作业
            /// 
            IList<EHSubmit.Model.Goal> goals = manager.GetGoalsOfStudent(CurrentUser.ID);

            int count = goals.Count;

            IsSuccess = true;

            if (count > 0)
            {
                ResponseText = "{" + string.Format("succcess:true,msg:您有{0}项作业还没提交", count) + "}";
            }
            else
            {
                ResponseText = "{" + string.Format("succcess:true,msg:目前所布置的作业都已提交") + "}";
            }
        }
        else
        {
            Message = "您没有权限执行此操作";
        }
    }

    public void GetWeather()
    {
        string[] Weather = new string[4];
        ServicesProvider Provider = new ServicesProvider();
        Weather = Provider.WeatherService();
        if (Weather[0] != "这城市或区域暂时不被支持")
        {
            IsSuccess = true;
            ResponseText = "{success:true,data:[{\"ImageSrc\":\"" + Weather[3] + "\",\"Temperature\":\"" + Weather[0] + "\",\"Weather\":\"" + Weather[1] + "\",\"Wind\":\"" + Weather[2] + "\"}]}";
        }
        else
        {
            Message = "网络错误";
        }
    }
}